home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / quad.cc < prev    next >
C/C++ Source or Header  |  1997-05-26  |  8KB  |  405 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <string>
  28.  
  29. #include <iostream.h>
  30.  
  31. #include "Quad.h"
  32.  
  33. #include "defun-dld.h"
  34. #include "error.h"
  35. #include "gripes.h"
  36. #include "help.h"
  37. #include "mappers.h"
  38. #include "pager.h"
  39. #include "pt-fvc.h"
  40. #include "oct-obj.h"
  41. #include "utils.h"
  42. #include "variables.h"
  43.  
  44. #if defined (quad)
  45. #undef quad
  46. #endif
  47.  
  48. // Global pointer for user defined function required by quadrature functions.
  49. static tree_fvc *quad_fcn;
  50.  
  51. static Quad_options quad_opts;
  52.  
  53. double
  54. quad_user_function (double x)
  55. {
  56.   double retval = 0.0;
  57.  
  58.   octave_value_list args;
  59.   args(0) = x;
  60.  
  61.   if (quad_fcn)
  62.     {
  63.       octave_value_list tmp = quad_fcn->eval (0, 1, args);
  64.  
  65.       if (error_state)
  66.     {
  67.       quad_integration_error = 1;  // XXX FIXME XXX
  68.       gripe_user_supplied_eval ("quad");
  69.       return retval;
  70.     }
  71.  
  72.       if (tmp.length () && tmp(0).is_defined ())
  73.     {
  74.       retval = tmp(0).double_value ();
  75.  
  76.       if (error_state)
  77.         {
  78.           quad_integration_error = 1;  // XXX FIXME XXX
  79.           gripe_user_supplied_eval ("quad");
  80.         }
  81.     }
  82.       else
  83.     {
  84.       quad_integration_error = 1;  // XXX FIXME XXX
  85.       gripe_user_supplied_eval ("quad");
  86.     }
  87.     }
  88.  
  89.   return retval;
  90. }
  91.  
  92. DEFUN_DLD (quad, args, nargout,
  93.   "[V, IER, NFUN] = quad (F, A, B [, TOL] [, SING])\n\
  94. \n\
  95. Where the first argument is the name of the  function to call to\n\
  96. compute the value of the integrand.  It must have the form\n\
  97. \n\
  98.   y = f (x)\n\
  99. \n\
  100. where y and x are scalars.\n\
  101. \n\
  102. The second and third arguments are limits of integration.  Either or\n\
  103. both may be infinite.\n\
  104. \n\
  105. The optional argument tol is a vector that specifies the desired\n\
  106. accuracy of the result.  The first element of the vector is the desired\n\
  107. absolute tolerance, and the second element is the desired relative\n\
  108. tolerance.\n\
  109. \n\
  110. The optional argument @var{sing} is a vector of values at which the\n\
  111. integrand is singular.")
  112. {
  113.   octave_value_list retval;
  114.  
  115.   int nargin = args.length ();
  116.  
  117.   if (nargin < 3 || nargin > 5 || nargout > 4)
  118.     {
  119.       print_usage ("quad");
  120.       return retval;
  121.     }
  122.  
  123.   quad_fcn = is_valid_function (args(0), "quad", 1);
  124.   if (! quad_fcn)
  125.     return retval;
  126.  
  127.   double a = args(1).double_value ();
  128.  
  129.   if (error_state)
  130.     {
  131.       error ("quad: expecting second argument to be a scalar");
  132.       return retval;
  133.     }
  134.  
  135.   double b = args(2).double_value ();
  136.  
  137.   if (error_state)
  138.     {
  139.       error ("quad: expecting third argument to be a scalar");
  140.       return retval;
  141.     }
  142.  
  143.   int indefinite = 0;
  144.   IndefQuad::IntegralType indef_type = IndefQuad::doubly_infinite;
  145.   double bound = 0.0;
  146.   if ((int) xisinf (a) && (int) xisinf (b))
  147.     {
  148.       indefinite = 1;
  149.       indef_type = IndefQuad::doubly_infinite;
  150.     }
  151.   else if ((int) xisinf (a))
  152.     {
  153.       indefinite = 1;
  154.       bound = b;
  155.       indef_type = IndefQuad::neg_inf_to_bound;
  156.     }
  157.   else if ((int) xisinf (b))
  158.     {
  159.       indefinite = 1;
  160.       bound = a;
  161.       indef_type = IndefQuad::bound_to_inf;
  162.     }
  163.  
  164.   int ier = 0;
  165.   int nfun = 0;
  166.   double abserr = 0.0;
  167.   double val = 0.0;
  168.   double abstol = 1e-6;
  169.   double reltol = 1e-6;
  170.   ColumnVector tol (2);
  171.   ColumnVector sing;
  172.   int have_sing = 0;
  173.   switch (nargin)
  174.     {
  175.     case 5:
  176.       if (indefinite)
  177.     {
  178.       error("quad: singularities not allowed on infinite intervals");
  179.       return retval;
  180.     }
  181.  
  182.       have_sing = 1;
  183.  
  184.       sing = args(4).vector_value ();
  185.  
  186.       if (error_state)
  187.     {
  188.       error ("quad: expecting vector of singularities as fourth argument");
  189.       return retval;
  190.     }
  191.  
  192.     case 4:
  193.       tol = args(3).vector_value ();
  194.  
  195.       if (error_state)
  196.     {
  197.       error ("quad: expecting vector of tolerances as fifth argument");
  198.       return retval;
  199.     }
  200.  
  201.       switch (tol.capacity ())
  202.     {
  203.     case 2:
  204.       reltol = tol (1);
  205.  
  206.     case 1:
  207.       abstol = tol (0);
  208.       break;
  209.  
  210.     default:
  211.       error ("quad: expecting tol to contain no more than two values");
  212.       return retval;
  213.     }
  214.  
  215.     case 3:
  216.       if (indefinite)
  217.     {
  218.       IndefQuad iq (quad_user_function, bound, indef_type, abstol, reltol);
  219.       iq.set_options (quad_opts);
  220.       val = iq.integrate (ier, nfun, abserr);
  221.     }
  222.       else
  223.     {
  224.       if (have_sing)
  225.         {
  226.           DefQuad dq (quad_user_function, a, b, sing, abstol, reltol);
  227.           dq.set_options (quad_opts);
  228.           val = dq.integrate (ier, nfun, abserr);
  229.         }
  230.       else
  231.         {
  232.           DefQuad dq (quad_user_function, a, b, abstol, reltol);
  233.           dq.set_options (quad_opts);
  234.           val = dq.integrate (ier, nfun, abserr);
  235.         }
  236.     }
  237.       break;
  238.  
  239.     default:
  240.       panic_impossible ();
  241.       break;
  242.     }
  243.  
  244.   retval(3) = abserr;
  245.   retval(2) = nfun;
  246.   retval(1) = ier;
  247.   retval(0) = val;
  248.  
  249.   return retval;
  250. }
  251.  
  252. typedef void (Quad_options::*d_set_opt_mf) (double);
  253. typedef double (Quad_options::*d_get_opt_mf) (void);
  254.  
  255. #define MAX_TOKENS 2
  256.  
  257. struct QUAD_OPTIONS
  258. {
  259.   const char *keyword;
  260.   const char *kw_tok[MAX_TOKENS + 1];
  261.   int min_len[MAX_TOKENS + 1];
  262.   int min_toks_to_match;
  263.   d_set_opt_mf d_set_fcn;
  264.   d_get_opt_mf d_get_fcn;
  265. };
  266.  
  267. static QUAD_OPTIONS quad_option_table [] =
  268. {
  269.   { "absolute tolerance",
  270.     { "absolute", "tolerance", 0, },
  271.     { 1, 0, 0, }, 1,
  272.     Quad_options::set_absolute_tolerance,
  273.     Quad_options::absolute_tolerance, },
  274.  
  275.   { "relative tolerance",
  276.     { "relative", "tolerance", 0, },
  277.     { 1, 0, 0, }, 1,
  278.     Quad_options::set_relative_tolerance,
  279.     Quad_options::relative_tolerance, },
  280.  
  281.   { 0,
  282.     { 0, 0, 0, },
  283.     { 0, 0, 0, }, 0,
  284.     0, 0, },
  285. };
  286.  
  287. static void
  288. print_quad_option_list (ostream& os)
  289. {
  290.   print_usage ("quad_options", 1);
  291.  
  292.   os << "\n"
  293.      << "Options for quad include:\n\n"
  294.      << "  keyword                                  value\n"
  295.      << "  -------                                  -----\n\n";
  296.  
  297.   QUAD_OPTIONS *list = quad_option_table;
  298.  
  299.   const char *keyword;
  300.   while ((keyword = list->keyword) != 0)
  301.     {
  302.       os.form ("  %-40s ", keyword);
  303.  
  304.       double val = (quad_opts.*list->d_get_fcn) ();
  305.       if (val < 0.0)
  306.     os << "computed automatically";
  307.       else
  308.     os << val;
  309.  
  310.       os << "\n";
  311.       list++;
  312.     }
  313.  
  314.   os << "\n";
  315. }
  316.  
  317. static void
  318. set_quad_option (const string& keyword, double val)
  319. {
  320.   QUAD_OPTIONS *list = quad_option_table;
  321.  
  322.   while (list->keyword != 0)
  323.     {
  324.       if (keyword_almost_match (list->kw_tok, list->min_len, keyword,
  325.                 list->min_toks_to_match, MAX_TOKENS))
  326.     {
  327.       (quad_opts.*list->d_set_fcn) (val);
  328.  
  329.       return;
  330.     }
  331.       list++;
  332.     }
  333.  
  334.   warning ("quad_options: no match for `%s'", keyword.c_str ());
  335. }
  336.  
  337. static octave_value_list
  338. show_quad_option (const string& keyword)
  339. {
  340.   octave_value_list retval;
  341.  
  342.   QUAD_OPTIONS *list = quad_option_table;
  343.  
  344.   while (list->keyword != 0)
  345.     {
  346.       if (keyword_almost_match (list->kw_tok, list->min_len, keyword,
  347.                 list->min_toks_to_match, MAX_TOKENS))
  348.     {
  349.       return (quad_opts.*list->d_get_fcn) ();
  350.     }
  351.       list++;
  352.     }
  353.  
  354.   warning ("quad_options: no match for `%s'", keyword.c_str ());
  355.  
  356.   return retval;
  357. }
  358.  
  359. DEFUN_DLD (quad_options, args, ,
  360.   "quad_options (KEYWORD, VALUE)\n\
  361. \n\
  362. Set or show options for quad.  Keywords may be abbreviated\n\
  363. to the shortest match.")
  364. {
  365.   octave_value_list retval;
  366.  
  367.   int nargin = args.length ();
  368.  
  369.   if (nargin == 0)
  370.     {
  371.       print_quad_option_list (octave_stdout);
  372.       return retval;
  373.     }
  374.   else if (nargin == 1 || nargin == 2)
  375.     {
  376.       string keyword = args(0).string_value ();
  377.  
  378.       if (! error_state)
  379.     {
  380.       if (nargin == 1)
  381.         return show_quad_option (keyword);
  382.       else
  383.         {
  384.           double val = args(1).double_value ();
  385.  
  386.           if (! error_state)
  387.         {
  388.           set_quad_option (keyword, val);
  389.           return retval;
  390.         }
  391.         }
  392.     }
  393.     }
  394.  
  395.   print_usage ("quad_options");
  396.  
  397.   return retval;
  398. }
  399.  
  400. /*
  401. ;;; Local Variables: ***
  402. ;;; mode: C++ ***
  403. ;;; End: ***
  404. */
  405.